<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
class Login extends Controller
{
    // 登陆首页
    public function index(){
        if(session('?uid')){
            $this->redirect('admin/index/index');exit;
        }
    	return $this->fetch();
    }
    // 登陆表单处理
    public function login() {
    	// var_dump($request);
    	$map['mobile']=$this->request->param('username','2');
    	$res=Db::table('sys_user')->where($map)->find();
    	// if(!$res|| $res['password']!=$this->request->param('password','','md5')) return json(['msg'=>'账号或密码错误','status'=>200,'data'=>null]);
    	if(!$res|| $res['password']!=$this->request->param('password','','md5')) {
    		$this->error('账号或密码错误','login/index');
		}

    	// if (!$res['status_id']) return json(['msg'=>'用户被锁定','status'=>200,'data'=>null]);
    	if (!$res['status_id']) {
    		$this->error('用户被锁定','login/index');
    	}
        //获取用户角色
        $role=Db::table('sys_role_user')->where('user_id',$res['id'])->find();
        if(!$role) $this->error('用户未生效,请联系管理员','login/index');
        $role_name=Db::table('sys_role')->where('id',$role['role_id'])->value('role_name');
        // 设置session
        // dump($role['role_id']);die;
        session('role_id',$role['role_id']);
        session('role_name',$role_name);
        session('user_id',$res['id']);
    	session('uid',$res['user_no']);
		session('username',$res['real_name']);
		// return json(['msg'=>'登陆成功','status'=>200,'data'=>null]);
		$this->success('登陆成功','index/index');

    }
    // 登陆验证码
    public function verfiy(){

    }
}

